//
//  ImageViewController.m
//  B
//
//  Created by Sunpeng on 15/11/7.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "ImageViewController.h"
#import "ImageCollectionViewCell.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "PhotoDetailViewController.h"
#import "BaseView.h"
@interface ImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)MBProgressHUD *HUD;
@property(nonatomic, retain)NSMutableArray *imageArr;
@property(nonatomic, copy)NSString *url;
@property(nonatomic,assign)NSInteger next_Page;
@property(nonatomic,assign)BOOL Loading;
@property(nonatomic, retain)NSMutableArray *textArr;

@property(nonatomic, retain)NSMutableArray *timeArr;

@property(nonatomic, retain)NSMutableArray *photo_infoArr;

@property(nonatomic,retain)NSMutableArray *titleArray;

@property(nonatomic,retain)NSMutableArray *trip_idArray;
@end

@implementation ImageViewController
-(void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [super viewWillAppear:animated];
    }
//
//- (void) viewWillDisappear:(BOOL)animated {
//    
//    [super viewWillDisappear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = NO;
//            
//        }
//    }
//    
//    
//}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.PicArr=[[NSMutableArray alloc] init];
        self.imageArr = [NSMutableArray array];
        self.textArr = [NSMutableArray array];
        self.timeArr = [NSMutableArray array];
        self.photo_infoArr = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];
        self.trip_idArray =[NSMutableArray array];
        self.hidesBottomBarWhenPushed = YES;
    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor clearColor];
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
         self.title = @"图赏";
    //title颜色
    NSDictionary *dict=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes=dict;
    //创建瀑布流效果
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    //每一个item的大小
    flowLayout.itemSize=CGSizeMake(0.293*WIDTH, 0.165*HIGHT);
    //设置最小的行间距
    flowLayout.minimumLineSpacing = 10;
    //设置最小的列间距
    flowLayout.minimumInteritemSpacing = 0;
    //滑动的方向(垂直滑动),默认是垂直滑动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //边缘
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //创建瀑布流视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, WIDTH, HIGHT-64) collectionViewLayout:flowLayout];
    //签订两个协议,一个dataSource,一个delegate
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //把瀑布流视图添加到view上
    [self.view addSubview: self.collectionView];
    
    //设置瀑布流的背景颜色
    self.collectionView.backgroundColor=[UIColor whiteColor];
    //注册
    //第一个参数:需要指定注册的类型;第二个参数:重用标志
    [ self.collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.collectionView.tag=1002;
    //请求值
    self.next_Page=2;
    [self createData:2];
    self.Loading=NO;
    [self addHeader];
    [self addFooter];
}
-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)addHeader
{
    

    
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.next_Page = 2;
        self.Loading = NO;
        [self createData:2];
        
    }];
    [self.collectionView.header beginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
//    __weak typeof(self) myself=self;
//    
//    [self.collectionView addFooterWithCallback:^{
//        myself.Loading = YES;
//        [myself createData:myself.next_Page];
//    }];
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        self.Loading = YES;
        [self createData:self.next_Page];
        
    }];
}
-(void)createData:(NSInteger)pageNum
{
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld",self.typeNUmber, self.idString,(long)pageNum];
    self.next_Page=self.next_Page+18;
    [HTTPTool get:url body:nil httpResult:JSON success:^(id result) {
        
        if (self.Loading == NO) {
            [self.PicArr removeAllObjects];
        }
        NSDictionary *dic = result;
//        NSLog(@"dic=%@",dic);
        NSArray *dataArr=dic[@"items"];
            for (NSDictionary *temp in dataArr) {
            SecondDetailPicModel *cover = [[SecondDetailPicModel alloc] init];
            [cover setValuesForKeysWithDictionary:temp];
                NSLog(@"888%@",cover.trip_id);
            // self.trip_idArray =
            [self.PicArr addObject:cover];
               
            [self.imageArr addObject:temp[@"photo"]];
            [self.photo_infoArr addObject:temp[@"photo_info"]];
            [self.textArr addObject:temp[@"text"]];
            [self.timeArr addObject:temp[@"local_time"]];
            [self.titleArray addObject:temp[@"trip_name"]];
            
        }
        
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        [self.HUD removeFromSuperview];
        [self.collectionView reloadData];
        
        
        
    }
          failure:^(NSError *error) {
              //NSLog(@"%@",error);
          }];
    //菊花
    self.HUD=[[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
    } completionBlock:^{
        [self.HUD show:NO];
        
    }];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{NSLog(@"00%@",self.PicArr);
    return self.PicArr.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath] ;
    //创建model对象
    SecondDetailPicModel *coverPic = self.PicArr[indexPath.row];
    //用sdwebimage对imageview进行图片赋值
   // NSLog(@"777%@",coverPic.photo);
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:coverPic.photo]];
    NSURL *url = [NSURL URLWithString:coverPic.photo];
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    [cell.myImageView sd_setImageWithURL:url placeholderImage:image];
    
    cell.layer.transform=CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform=CATransform3DMakeScale(1, 1, 0.1);
    }];
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    
    PhotoDetailViewController *image=[[PhotoDetailViewController alloc] init];
    SecondDetailPicModel *cover = [self.PicArr objectAtIndex:indexPath.row];
    
    
    image.trip_id = cover.trip_id;
    image.bId=self.idString;
    
    image.type=self.typeNUmber;
    
    //    image.refresh = self.refresh;
    image.textArr = self.textArr;
    image.index = indexPath.row;
    image.titleArray = self.titleArray;
    image.titleText = [self.titleArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:image animated:NO];
   
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
